package com.zmn.oms.dubbo.impl.zmn.normal.erp;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.common.utils.validator.ValidateResult;
import com.zmn.common.utils.validator.ValidatorUtil;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.dto.zmn.erp.OmsErpSyncAccountDIO;
import com.zmn.oms.dubbo.interfaces.zmn.normal.erp.ZsErpAccountModifyRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.work.modify.ZsSyncAccountDIO;
import com.zmn.oms.zmn.normal.business.interfaces.work.financework.ZsNormalErpFinanceWorkBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author sunlife
 * @date:2020/9/16 11:09 上午
 * description:
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ZsErpAccountModifyRemoteServiceImpl implements ZsErpAccountModifyRemoteService {

    private static final String TAG = "erp同步";

    @Resource
    private ZsNormalErpFinanceWorkBService zsNormalErpFinanceWorkBService;

    @Override
    public ResponseDTO syncOrderAccountResult(@Valid OmsErpSyncAccountDIO dio) {
        log.info("[{}] 算账入参：{}", TAG, JSONObject.toJSONString(dio));
        ValidateResult validateResult = ValidatorUtil.validator(dio);
        if (!validateResult.isSuccess()) {
            log.error("[{}] 参数错误：{}", TAG, validateResult.toString());
            return ResponseDTO.fail("提交参数错误：" + validateResult.toString());
        }
        try {
            ZsSyncAccountDIO accountDIO = BeanMapper.map(dio, ZsSyncAccountDIO.class);
            zsNormalErpFinanceWorkBService.syncAccount(accountDIO);
        } catch (OmsBaseException oe) {
            log.error("#{}同步算账结果错误：{}", dio.getWorkId(), oe.getMessage());
            return ResponseDTO.fail(oe.getMessage());
        } catch (Exception e) {
            log.error("#{}同步算账结果错误：{}", dio.getWorkId(), e.getMessage());
            return ResponseDTO.fail("同步失败，请稍后重试");
        }

        return ResponseDTO.success();
    }
}
